<template>
  <div class="qtSuppliers">
   <qtHeader fatherTitle="供应商" fatherCreate="创建新的" @tell='getName'></qtHeader>
    <el-row>
      <el-col :span="18">
        <el-table :data="tableData" stripe style="width: 100%">
          <el-table-column prop="suppliersName" label="供应商名称" width="200"></el-table-column>
          <el-table-column prop="suppliersAddress" label="供应商地址" width="400"></el-table-column>
          <el-table-column prop="contactName" label="联系人姓名" width="150"></el-table-column>
          <el-table-column prop="email" label="电子邮件" width="150"></el-table-column>
          <el-table-column prop="phone" label="联系方式" width="150"></el-table-column>
          <el-table-column label="操作" >
            <template slot-scope="scope">
              <el-button size="mini" type="warning" @click="handleEdit(scope.row)">
                <i class="el-icon-edit"></i>
              </el-button>
              <el-button size="mini" type="danger" @click="handleDelete(scope.row)">
                <i class="el-icon-delete"></i>
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
      <el-col :span="6"></el-col>
    </el-row>
     <!-- 添加弹框 -->
    <el-dialog  :visible.sync="dialogFormVisible" width="800px"  center custom-class="dialogClass" @close="cancel">
      <el-form :model="form" ref="dataform">
        <el-form-item label="供应商名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="供应商地址" :label-width="formLabelWidth" prop="address">
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系人姓名" :label-width="formLabelWidth" prop="contactName">
          <el-input v-model="form.contactName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系方式" :label-width="formLabelWidth" prop="phone">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电子邮件" :label-width="formLabelWidth" prop="email">
          <el-input v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getSuppliers, addSuppliers, delSuppliers, checkSuppliers } from '@/api/index.js'
import qtHeader from '../components/header'
export default {
  components: {
    qtHeader
  },
  data () {
    return {
      tableData: [],
      form: {
        name: '',
        address: '',
        contactName: '',
        phone: '',
        email: ''
      },
      dialogFormVisible: false,
      formLabelWidth: '200px',
      row: '',
      assetsId: ''
    }
  },
  created () {
    this.getSuppliers()
  },
  methods: {
    getSuppliers () {
      getSuppliers().then(res => {
        // console.log(res)
        if (res.data.msg === 'success') {
          this.tableData = res.data.data
        }
      })
    },
    getName (name) {
      this.dialogFormVisible = name
    },
    confirm () {
      var obj = {
        name: this.form.name,
        address: this.form.address,
        contactName: this.form.contactName,
        phone: this.form.phone,
        email: this.form.email
      }
      if (this.assetsId) {
        obj.id = this.assetsId
        if (this.row.contactName !== this.form.contactName) {
          obj.contactName = this.form.contactName
        } else {
          obj.contactName = this.row.contactName
        }
        if (this.row.suppliersName !== this.form.name) {
          obj.name = this.form.name
        } else {
          obj.name = this.row.suppliersName
        }
        if (this.row.email !== this.form.email) {
          obj.email = this.form.email
        } else {
          obj.email = this.row.email
        }
        if (this.row.phone !== this.form.phone) {
          obj.phone = this.form.phone
        } else {
          obj.phone = this.row.phone
        }
        if (this.row.suppliersAddress !== this.form.address) {
          obj.address = this.form.address
        } else {
          obj.address = this.row.suppliersAddress
        }
        checkSuppliers(obj).then(res => {
          if (res.data.code === 200) {
            this.$message.success(res.data.msg)
            this.getSuppliers()
          } else {
            this.$message.error(res.data.msg)
          }
        })
      } else {
        addSuppliers(obj).then(res => {
          if (res.data.code === 200) {
            this.$message.success('新建供应商成功')
            this.getSuppliers()
          } else {
            this.$message.error('新建供应商失败')
          }
        })
      }

      this.dialogFormVisible = false
    },
    cancel () {
      this.assetsId = ''
      this.$refs.dataform.resetFields()
      for (var key in this.form) {
        this.form[key] = ''
      }
      this.dialogFormVisible = false
    },
    handleDelete (row) {
      this.$confirm('是否删除资产?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delSuppliers(row.id).then(res => {
          if (res.data.code === 200) {
            this.$message.success(res.data.msg)
          } else {
            this.$message.error(res.data.msg)
          }
          // 判断当删除的数据为当前页中的最后一条数据，刷新页面的时候需要把当前页码数减1
          if (this.total / this.pageSize - parseInt(this.total / this.pageSize) <= 0.1) {
            this.page = this.page - 1
          }
          this.getSuppliers()
        })
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    handleEdit (row) {
      this.getName(name)
      this.row = row
      // console.log(row)
      this.assetsId = row.id
      this.form.name = row.suppliersName
      this.form.address = row.suppliersAddress
      this.form.contactName = row.contactName
      this.form.phone = row.phone
      this.form.email = row.email
    }
  }
}
</script>

<style lang="scss" scoped>
.qtSuppliers {
  padding: 10px;
}
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.qtTitle {
  font-size: 25px;
  text-align: left;
  line-height: 1;
}
.qtButton {
  margin-top: 5px;
  text-align: right;
  padding-right: 20px;
  .qtCreateAsset {
    background-color: #3c8dbc;
    padding: 5px 10px;
    color: #fff;
    border-radius: 5px;
  }
}
</style>
